﻿<%@ WebHandler Language="C#" Class="ContractPrintMgr" %>

using System;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Data;
using Chenxin.Lib.Utility;
using Chenxin.Lib.Business;
using Chenxin.Lib.Business.BLL;

public class ContractPrintMgr : AjaxHandlerBase
{
    public ContractPrintMgr()
    {
        //添加报备权限
        htTypePermit.Add("print", Constants.Permit_ContractRepeatPrint);
    }
    override public void RequestProcess(HttpContext context)
    {
        try
        {
            context.Response.ContentType = "text/plain";
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";

            string strOp = context.Request["op"];
            switch (strOp.ToLower())
            {
                //添加合同
                case "add":
                    Add(context);
                    break;
                case "print":
                    Print(context);
                    break;
                case "edit":
                    Edit(context);
                    break;
                case "getlist":
                    GetList(context);
                    break;
                case "getmodel":
                    GetModel(context);
                    break;
                default:
                    break;
            }
        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }
    }
    private void Print(HttpContext context)
    {
        try
        {
            Chenxin.Lib.Business.Entity.ContractPrintInfo modle = (Chenxin.Lib.Business.Entity.ContractPrintInfo)JsonHelp.Convert2Entity(context.Request["model"], typeof(Chenxin.Lib.Business.Entity.ContractPrintInfo));

            modle.ContractPrint_ID = Guid.NewGuid();
            modle.ContractPrint_Date = DateTime.Now;
            modle.User_Code = CurrentUserInfo.UserCode;
            modle.User_ID = CurrentUserInfo.UserId;
            modle.User_Name = CurrentUserInfo.UserName;
            Chenxin.Lib.Business.DAL.ContractPrintInfo.Add(modle);



            //更新合同打印次数及最后打印人信息
            Chenxin.Lib.Business.Entity.ContractInfo modleContract = Chenxin.Lib.Business.DAL.ContractInfo.GetModel(modle.Contract_Id);
            modleContract.Contract_PrintCount += 1;
            modleContract.Contract_LastPrintDate = modle.ContractPrint_Date;
            modleContract.Contract_LastPrintUserId = modle.User_ID;
            modleContract.Contract_LastPrintUserName = modle.User_Name;
            Chenxin.Lib.Business.DAL.ContractInfo.Update(modleContract);

            context.Response.Write(string.Format("{{success:1,msg:'保存成功！',data:{{contractprint_id:'{0}'}}}}", modle.ContractPrint_ID));
        }
        catch (Exception e)
        {
            context.Response.Write(string.Format("{{success:0,msg:'保存失败！原因：{0}',data:''}}", e.Message.Replace("'", "\"")));
        }
    }
    
    private void Edit(HttpContext context)
    {
        try
        {
            Chenxin.Lib.Business.Entity.ContractPrintInfo modle = (Chenxin.Lib.Business.Entity.ContractPrintInfo)JsonHelp.Convert2Entity(context.Request["model"], typeof(Chenxin.Lib.Business.Entity.ContractPrintInfo));

            Chenxin.Lib.Business.DAL.ContractPrintInfo.Update(modle);

            context.Response.Write(string.Format("{{success:1,msg:'保存成功！',data:{{contractprint_id:'{0}'}}}}", modle.ContractPrint_ID));
        }
        catch (Exception e)
        {
            context.Response.Write(string.Format("{{success:0,msg:'保存失败！原因：{0}',data:''}}", e.Message.Replace("'", "\"")));
        }        
    }
    
    private void GetModel(HttpContext context)
    {
        try
        {
            Chenxin.Lib.Business.Entity.ContractPrintInfo model = Chenxin.Lib.Business.DAL.ContractPrintInfo.GetModel(new Guid(context.Request["modelid"]));
            context.Response.Write(string.Format("{{success:1,msg:'',data:{{model:{0}}}}}", JsonHelp.Convert2Json(model)));
        }
        catch
        {
            context.Response.Write("{success:0,msg:'获取记录信息失败！',data:''}");
        }
    }

    private void GetList(HttpContext context)
    {
        try
        {

            string strWhere = "1=1";
            string order = "ContractPrint_Date desc";

            int pageSize = 10;
            int pageIndex = 1;
            int count = 0;

            if (context.Request["pagesize"] != null)
            {
                pageSize = Convert.ToInt32(context.Request["pagesize"]);
            }
            if (context.Request["pageindex"] != null)
            {
                pageIndex = Convert.ToInt32(context.Request["pageindex"]) + 1;
            }

            if (context.Request["contractcode"] != null)
            {
                strWhere += string.Format(" and Contract_Code like '%{0}%'", context.Request["contractcode"]);
            }
            if (context.Request["usercode"] != null)
            {
                strWhere += string.Format(" and User_Code like '%{0}%'", context.Request["usercode"]);
            }
            if (context.Request["endtime"] != null && context.Request["starttime"] != null)
            {
                strWhere += string.Format(" and (ContractPrint_Date>'{0}' and ContractPrint_Date<'{1}')", context.Request["endtime"], context.Request["starttime"]);                
            }
            if (context.Request["sort"] != null && context.Request["sort"] != "")
            {
                order = context.Request["sort"];
            }

            DataSet ds = Chenxin.Lib.Business.DAL.ContractPrintInfo.GetList(pageIndex, pageSize, order, strWhere, "*", "*", true);
            count = Convert.ToInt32(ds.Tables[1].Rows[0].ItemArray[0]);
            context.Response.Write(string.Format("{{success:1,msg:'',data:{{count:{0},{1},strWhere:'{2}'}}}}", count, JsonHelp.Convert2Json(ds.Tables[0]),strWhere.Replace("'","\"")));
            
        }
        catch (Exception e)
        {
            context.Response.Write(string.Format("{{success:0,msg:'加载失败！原因：{0}',data:''}}", e.Message.Replace("'", "\"")));
        }
    }
    private void Add(HttpContext context)
    {
        try
        {
            Chenxin.Lib.Business.Entity.ContractPrintInfo modle = (Chenxin.Lib.Business.Entity.ContractPrintInfo)JsonHelp.Convert2Entity(context.Request["model"], typeof(Chenxin.Lib.Business.Entity.ContractPrintInfo));

            modle.ContractPrint_ID = Guid.NewGuid();
            modle.ContractPrint_Date = DateTime.Now;
            modle.User_Code = CurrentUserInfo.UserCode;
            modle.User_ID = CurrentUserInfo.UserId;
            modle.User_Name = CurrentUserInfo.UserName;
            Chenxin.Lib.Business.DAL.ContractPrintInfo.Add(modle);

            context.Response.Write(string.Format("{{success:1,msg:'保存成功！',data:{{contract_id:'{0}'}}}}", modle.Contract_Id));
        }
        catch(Exception e)
        {
            context.Response.Write(string.Format("{{success:0,msg:'保存失败！原因：{0}',data:''}}", e.Message.Replace("'", "\"")));
        }
    }


}